/* Filename: rcver.c
 * Description: 消息队列，接收方
 * ipcs:ipc show
 * ipcrm:ipc remove
 * Last modified: humble 20200504 14:57
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include "proto.h"

static void usage(void);

int main(int argc, char **argv)
{
    key_t key;
    int msgid;
    msg_t rbuf;

    if(argc != 1){
        fprintf(stderr, "argc !=1");
        usage();
        exit(1);
    }

    key = ftok(KEYPATH, KEYPROJ);
    if(key < 0){
        perror("ftok()");
        exit(1);
    }

    msgid = msgget(key, IPC_CREAT|0600);
    if(msgid < 0){
        perror("msgget()");
        exit(1);
    }

    while(1){
        if(msgrcv(msgid, &rbuf, sizeof(rbuf) - sizeof(long), 0, 0) < 0){
            perror("msgrcv()");
            exit(1);
        }
        printf("name:%s\n", rbuf.name);
        printf("math:%d\n", rbuf.math);
        printf("chinese:%d\n", rbuf.chinese);
    }

    if(msgctl(msgid, IPC_RMID, NULL) < 0){
        perror("msgctl()");
        exit(1);
    }

    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
